var deviceone = require("deviceone");
var cacher = deviceone.sm("do_DataCache");
var openHelper = require("helper/open");
var circleUrl = require("url/circle");
var http = require("helper/http");
/**
 * 存取“最后查看圈子的时间”
 */
var last_seen_circle_key = "circleLast@SeenTime@circleId_";
module.exports.cacheLastTime4Circle = function(){
	if(arguments.length==0){
		return;
    }else if(arguments.length==1){
    	var circleId = arguments[0];
    	if(cacher.hasData(last_seen_circle_key+""+circleId)){
			var lastValue = cacher.loadData(last_seen_circle_key+""+circleId);
			if(!lastValue || lastValue==null || lastValue==="undefined"){
				var now = new Date().getTime() - (1 * 60 * 60 * 1000);//一小时前
				cacher.saveData({key: last_seen_circle_key+""+circleId, value: now});
				return now;
			}
			return lastValue;
		}else{
			var now = new Date().getTime() - (1 * 60 * 60 * 1000);//一小时前
			cacher.saveData({key: last_seen_circle_key+""+circleId, value: now});
			return now;
		}
    }else if(arguments.length==2){
    	var circleId = arguments[0];
    	var lastValue = arguments[1];
    	if(circleId && circleId!=null && circleId!="undefined"){
    		cacher.saveData({key: last_seen_circle_key+""+circleId, value: lastValue});
    	}
    }
};

/**
 * 打开圈子详情页面
 */
module.exports.toCircleDetail = function(circleId,circleName){
	if(circleId!=null){
		if(!circleName || circleName == null || circleName==="undefined"){
			circleName = "";
		}
		openHelper.open("source://view/circle/base/detail/circleDetail.ui",{"circleId":circleId,"circleName":circleName});
	}
};

//屏蔽话题
module.exports.hideTopic = function(topicId,callBack){
	if(topicId==null && topicId=="undefined"){
		nf.toast("话题id为空");
		return;
	}
	http.post(circleUrl.url.topic.hide, {id: topicId}, function(data) {
		if(callBack && callBack!=null && callBack!="undefined"){
			callBack();
		}else{
			nf.toast("屏蔽话题成功");
		}
	});
}

//屏蔽话题
module.exports.deleteTopic = function(topicId,callBack){
	if(topicId==null && topicId=="undefined"){
		nf.toast("话题id为空");
		return;
	}
	http.post(circleUrl.url.topic.doDelete, {id: topicId}, function(data) {
		if(callBack && callBack!=null && callBack!="undefined"){
			callBack();
		}else{
			nf.toast("删除话题成功");
		}
	});
}

//退出圈子或群聊
module.exports.quitCircle = function(circleId,callBack){
	if(circleId==null && circleId=="undefined"){
		nf.toast("圈子id为空");
		return;
	}
	http.post(circleUrl.url.member.quitCircle, {circleId: circleId}, function(data) {
		if(callBack && callBack!=null && callBack!="undefined"){
			callBack();
		}else{
			nf.toast("退出圈子成功");
		}
	});
}

//解散圈子或群聊
module.exports.dismissCircle = function(circleId,callBack){
	if(circleId==null && circleId=="undefined"){
		nf.toast("圈子id为空");
		return;
	}
	http.post(circleUrl.url.base.dismiss, {id: circleId}, function(data) {
		if(callBack && callBack!=null && callBack!="undefined"){
			callBack();
		}else{
			nf.toast("退出圈子成功");
		}
	});
}